package com.kk.controller;

import com.kk.entity.LeaveApplication;
import com.kk.entity.User;
import com.kk.service.LeaveApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/counselor")
public class CounselorController {
    
    @Autowired
    private LeaveApplicationService leaveApplicationService;
    
    @GetMapping("/index")
    public String index(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !"COUNSELOR".equals(user.getRole())) {
            return "redirect:/login";
        }
        
        // 添加用户信息到Model
        model.addAttribute("user", user);
        
        // 获取待审核的申请（校医院已通过且学生已选择时间的申请）
        List<LeaveApplication> pendingApplications = 
            leaveApplicationService.getPendingApplicationsForCounselor();
        model.addAttribute("pendingApplications", pendingApplications);
        
        // 获取已处理的申请
        List<LeaveApplication> processedApplications = 
            leaveApplicationService.getProcessedApplicationsByCounselor(user.getId());
        model.addAttribute("processedApplications", processedApplications);
        
        return "counselor/index";
    }
    
    @PostMapping("/review/{id}")
    @ResponseBody
    public ResponseEntity<?> reviewApplication(
            @PathVariable Long id,
            @RequestParam String result,
            @RequestParam String comment,
            HttpSession session) {
        
        User user = (User) session.getAttribute("user");
        if (user == null || !"COUNSELOR".equals(user.getRole())) {
            return ResponseEntity.badRequest().build();
        }
        
        boolean success = leaveApplicationService.reviewApplicationByCounselor(id, user.getId(), result, comment);
        
        if (success) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }
} 